<?php

/**
 * @Author: DEEP-2018BRTYLP\Administrator
 * @Date:   2019-04-09 16:15:45
 * @Last Modified by:   DEEP-2018BRTYLP\Administrator
 * @Last Modified time: 2019-04-09 16:53:05
 */
namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use app\common\controller\WeiXin;

class Frog extends Frontend
{  
   public function index()
   {  
      $weixin = new WeiXin();
      $current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
      if(!isset($_GET['code'])){
         $weixin->get_author($current_url);
      }else{
         $code = $_GET['code'];
         file_put_contents("signature.txt",'获取code:'.date("Y-m-d:H:i:s",time()).$_GET['code'].PHP_EOL,FILE_APPEND);
         $weixin->get_author_openid($code);
      }
      return $this->view->fetch();
   }
   public function rank(){
      $uid = $this->get_uid();
      $info = db('member')->where(['id'=>$uid])->field('headimgurl,wechat_name')->find();
      //$sql = "select m.wechat_name as wechat_name ,m.headimgurl as headimgurl,s.uid,max(s.score) as score,s.createtime FROM fa_frog_score as s INNER JOIN fa_member as m ON s.uid=m.id group by s.uid order by score asc limit 50";
      //$sql = "select uid,max(score) as score,createtime FROM fa_frog_score group by uid order by score asc limit 50";
      $sql = "select table_score.score,table_score.uid,table_score.createtime,m.headimgurl,m.wechat_name from fa_member as m join (select uid,max(score) as score,createtime FROM fa_frog_score  group by uid  limit 50) as table_score on table_score.uid=m.id";
      $data = db()->query($sql);
      $order_data = [];
      foreach ($data as $key => &$value) {
         $order_data[] = $value['uid'];
         $value['createtime'] = date('Y-m-d:H:i:s',$value['createtime']);
      }
      $score = array_column($data,'score');
      array_multisort($score,SORT_DESC,$data);
      $key = array_search($uid, $order_data);
      if($key==false){
         $order = "未上榜";
      }else{
         $order = $key+1;
      }
      //dump($data);exit;
      $default_img = 'http://'.$_SERVER['HTTP_HOST']."/static/images/people1.jpg";
      $this->assign("default_img",$default_img);
      $this->assign('data',$data);
      $this->assign('order',$order);
      $this->assign('info',$info);
      return $this->view->fetch();
   }

   public function save_score(){
      $uid = $this->get_uid();
      $score = input('param.score');
      $data = [
         'uid' => $uid,
         'score' => $score,
         'createtime' => time(),
      ];
      file_put_contents("signature.txt",'save_score:'.date("Y-m-d:H:i:s",time())."____".json_encode($data).PHP_EOL,FILE_APPEND);
      if($this->is_user($uid)){
         $re = db('frog_score')->insert($data);
         return 'ok';
      }else{
         return false;
      };
     
   }

   public function get_top(){
      $uid = $this->get_uid();
      $score = db('frog_score')->where(['uid'=>$uid])->max('score');
      if(!$score){
         $score = 0;
      }
      return $score;
   }
   public function test(){
      $uid = $this->get_uid();

   }

   public function get_uid(){
      if(session('uid')){
         $uid = session('uid');
      }else{
         $weixin = new WeiXin();
         $uid = $weixin->get_frog_uid();
      }
      return $uid;
   }

   /**
    * 判断该用户是否存在
    */
   public function is_user($uid){
      $re = db('member')->where(['id'=>$uid])->find();
      if($re){
         return true;
      }else{
         return false;
      }
   }

}
